package com.hst.web.base.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.dao.IORDao;
import com.hst.core.dao.ISQLDao;
import com.hst.core.dao.ORQuery;
import com.hst.core.dao.ORQuery.Op;
import com.hst.core.json.JsonUtil;
import com.hst.web.base.entity.TFlowHistory;
import com.hst.web.base.service.VehicleCardService;
import com.hst.web.base.ui.CompanyVehicle;
import com.hst.web.base.ui.FlowTaskVehicle;
import com.hst.web.base.ui.VehicleCardApprove;

@Controller
@RequestMapping("/vehiclecard")
public class VehicleCardController {
	
	@Autowired
	ISQLDao sql;

	@Autowired
	IORDao dao;
	
	@Autowired
	VehicleCardService service;
	
	@SuppressWarnings({ "unchecked" })
	private Map<String, Object> parseQuerys(String data) throws JsonParseException, JsonMappingException, IOException {
		Map<String, Object> querys = JsonUtil.read(data, Map.class);
		return querys;
	}
	
	@RequestMapping("/applyCard")
	public void applyCard(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String,Object> params = parseQuerys(data);
		String cardType = (String) params.get("cardType");
		String remark = (String) params.get("remark");
		String vechileStr = (String) params.get("vechiles");
		List<CompanyVehicle> emps = JsonUtil.readList(vechileStr, CompanyVehicle.class);
		service.applyCard(cardType, remark, emps, m);
	}
	
	@RequestMapping("/taskHistory")
	public void taskHistory(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		FlowTaskVehicle task = JsonUtil.read(data, FlowTaskVehicle.class);
		List<ORQuery> querys = new ArrayList<ORQuery>();
		querys.add(new ORQuery(Op.eq,"taskId",task.getId()));
		querys.add(new ORQuery(Op.order,"dt","asc"));
		List<TFlowHistory> historys = dao.list(TFlowHistory.class,querys);
		m.addAttribute("historys",JsonUtil.toString(historys));
	}
	
	@RequestMapping("/listApplyDetail")
	public void listApplyDetail(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		VehicleCardApprove approve = JsonUtil.read(data, VehicleCardApprove.class);
		service.listApplyDetail(approve, m);
	}
	
	@RequestMapping("/listApplyDetailView")
	public void listApplyDetailView(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		VehicleCardApprove approve = JsonUtil.read(data, VehicleCardApprove.class);
		service.listApplyDetailView(approve, m);
	}	
	
	@RequestMapping("/back")
	public void back(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String,Object> params = parseQuerys(data);
		String approveStr = (String) params.get("task");
		String remark = (String) params.get("remark");
		VehicleCardApprove approve = JsonUtil.read(approveStr, VehicleCardApprove.class);
		service.back(approve,remark,m);
	}
	
	@RequestMapping("/pass")
	public void pass(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		Map<String,Object> params = parseQuerys(data);
		String approveStr = (String) params.get("task");
		String remark = (String) params.get("remark");
		VehicleCardApprove approve = JsonUtil.read(approveStr, VehicleCardApprove.class);
		service.pass(approve, m, remark);
	}
	
	@RequestMapping("/backCheckStatus")
	public void backCheckStatus(@RequestBody String data, Model m) throws JsonParseException, JsonMappingException, IOException {
		List<CompanyVehicle> vehicles = JsonUtil.readList(data, CompanyVehicle.class);
		service.backCheckStatus(vehicles, m);
	}
}
